﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Collections;

namespace SMALLERP.PM
{
    public partial class DDKC : Form
    {
     DataBase db = new DataBase();
       public string kclx;
       public string gzzx;
       public string kctj;
       public DataView dv;
        public DDKC()
        {
            InitializeComponent();
        }
        public delegate void CheckBoxClickedHandler(bool state);
        public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
        {
            bool _bChecked;
            public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
            {
                _bChecked = bChecked;
            }
            public bool Checked
            {
                get { return _bChecked; }
            }
        }
        class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
        {
            Point checkBoxLocation;
            Size checkBoxSize;
            bool _checked = false;
            Point _cellLocation = new Point();
            System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
            public event CheckBoxClickedHandler OnCheckBoxClicked;

            public DatagridViewCheckBoxHeaderCell()
            {
            }

            protected override void Paint(System.Drawing.Graphics graphics,
                System.Drawing.Rectangle clipBounds,
                System.Drawing.Rectangle cellBounds,
                int rowIndex,
                DataGridViewElementStates dataGridViewElementState,
                object value,
                object formattedValue,
                string errorText,
                DataGridViewCellStyle cellStyle,
                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                    dataGridViewElementState, value,
                    formattedValue, errorText, cellStyle,
                    advancedBorderStyle, paintParts);
                Point p = new Point();
                Size s = CheckBoxRenderer.GetGlyphSize(graphics,
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
                p.X = cellBounds.Location.X +
                    (cellBounds.Width / 2) - (s.Width / 2);
                p.Y = cellBounds.Location.Y +
                    (cellBounds.Height / 2) - (s.Height / 2);
                _cellLocation = cellBounds.Location;
                checkBoxLocation = p;
                checkBoxSize = s;
                if (_checked)
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.CheckedNormal;
                else
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.UncheckedNormal;
                CheckBoxRenderer.DrawCheckBox
                (graphics, checkBoxLocation, _cbState);
            }

            protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
            {
                Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
                if (p.X >= checkBoxLocation.X && p.X <=
                    checkBoxLocation.X + checkBoxSize.Width
                && p.Y >= checkBoxLocation.Y && p.Y <=
                    checkBoxLocation.Y + checkBoxSize.Height)
                {
                    _checked = !_checked;
                    if (OnCheckBoxClicked != null)
                    {
                        OnCheckBoxClicked(_checked);
                        this.DataGridView.InvalidateCell(this);
                    }

                }
                base.OnMouseClick(e);
            }
        }

        public String UserId
        {
            get
            {
                return _UserId;
            }
        }
        String _UserId;


        /// <summary>
        /// 用户名
        /// </summary>
        public String UserName
        {
            get
            {
                return _UserName;
            }
        }
        String _UserName;

        public List<String> UserIds
        {
            get
            {
                return _UserIds;
            }
        }
        List<String> _UserIds =new List<string>();


        /// <summary>
        /// 用户名
        /// </summary>
        public List<String> UserNames
        {
            get
            {
                return _UserNames;
            }
        }
        List<String> _UserNames=new List<string>();


        public event EventHandler DataSelected;

        private void DDKC_Load(object sender, EventArgs e)
        {
            if (kclx=="yuangongfx")
            {
                DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
                DatagridViewCheckBoxHeaderCell bbHeader = new DatagridViewCheckBoxHeaderCell();
                colCB.HeaderCell = bbHeader;
                colCB.Name = "colCB";
                colCB.HeaderText = "";
                colCB.DataPropertyName = "选择";
                dataGridView1.Columns.Insert(0, colCB);
                bbHeader.OnCheckBoxClicked +=
                    new CheckBoxClickedHandler(abHeader_OnCheckBoxClicked);
                button1.Visible = true;
            }

            string strSql = null;
            switch (kclx)
            {
                case "yuangong":
                    strSql = "SELECT MV001 员工编号,MV002 员工姓名 FROM CMSMV";
                    break;
                case "yuangongfx":
                    strSql = "SELECT 'false' AS 选择,MV001 员工编号,MV002 员工姓名 FROM CMSMV";
                    break;
                case "zhuangmogong":
                    strSql = "SELECT MV001 员工编号,MV002 员工姓名 FROM CMSMK LEFT JOIN CMSMV ON MK002=MV001 LEFT JOIN CMSMJ ON MK001=MJ001 WHERE RTRIM(MJ002)='D'";
                    break;
                case "gongyingshang":
                    strSql = "SELECT MA001 供应商编号,MA002 供应商名称 FROM PURMA";
                    break;
                case "sbz":
                    strSql = "SELECT DISTINCT RTRIM(UDF02) 设备组,RTRIM(UDF02) 设备组名称 FROM CMSMX WHERE MX002='" + gzzx + "'";
                    break;
                case "shebei":
                    strSql = "SELECT DISTINCT RTRIM(MX001) 设备编号 FROM CMSMX WHERE MX002='" + gzzx + "' AND UDF02='" + kctj + "'";
                    break;
                case "gongyi":
                    strSql = "SELECT OPERATION_CODE 工艺,OPERATION_NAME 工艺名称 FROM OPERATION";
                    this.Text = "工艺选择";
                    break;
                default:
                    break;
            }
           
            try
            {
                db = new DataBase();

                DataTable dt = db.GetDataSet1(strSql, "CMSMV").Tables["CMSMV"]; 
                dv = dt.DefaultView;
                dataGridView1.DataSource = dv;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
        }

        private void abHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态
            dataGridView1.EndEdit();
            dataGridView1.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string strSql = null;
            if (kclx != "yuangongfx")
            {
                switch (kclx)
                {
                    case "yuangong":
                        strSql = "SELECT MV001 员工编号,MV002 员工姓名 FROM CMSMV WHERE (MV002 LIKE '%" + textBox1.Text + "%' OR MV001 LIKE '%" + textBox1.Text + "%')";
                        break;
                    case "zhuangmogong":
                        strSql = "SELECT MV001 员工编号,MV002 员工姓名 FROM CMSMK LEFT JOIN CMSMV ON MK002=MV001 LEFT JOIN CMSMJ ON MK001=MJ001 WHERE RTRIM(MJ002)='D' AND  (MV002 LIKE '%" + textBox1.Text + "%' OR MV001 LIKE '%" + textBox1.Text + "%')";
                        break;
                    case "gongyingshang":
                        strSql = "SELECT MA001 供应商编号,MA002 供应商名称 FROM PURMA WHERE (MA002 LIKE '%" + textBox1.Text + "%' OR MA001 LIKE '%" + textBox1.Text + "%')";
                        break;
                    case "sbz":
                        strSql = "SELECT DISTINCT RTRIM(UDF02) 设备组,RTRIM(UDF02) 设备组名称 FROM CMSMX WHERE UDF02 LIKE '%" + textBox1.Text + "%' AND MX002='" + gzzx + "'";
                        break;
                    case "shebei":
                        strSql = "SELECT DISTINCT RTRIM(MX001) 设备编号 FROM CMSMX WHERE MX002='" + gzzx + "' AND UDF02='" + kctj + "' AND MX001 LIKE '%" + textBox1.Text + "%'";
                        break;
                    case "gongyi":
                        strSql = "SELECT OPERATION_CODE 工艺,OPERATION_NAME 工艺名称 FROM OPERATION WHERE OPERATION_NAME LIKE '%" + textBox1.Text + "%'";
                        break;
                    default:
                        break;
                }

                try
                {                  
                    dataGridView1.DataSource = db.GetDataSet1(strSql, "CMSMV").Tables["CMSMV"];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
            else
            {
                dv = (DataView)dataGridView1.DataSource;
                dv.RowFilter = "(员工姓名 LIKE '%" + textBox1.Text + "%' OR 员工编号 LIKE '%" + textBox1.Text + "%')";
                dataGridView1.DataSource = dv;
            }
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.DataSelected != null)
            {
              
              
                switch (kclx)
                {
                    case "yuangong":                                                                       
                                this._UserId=dataGridView1.Rows[e.RowIndex].Cells["员工编号"].Value.ToString();
                                this._UserName=dataGridView1.Rows[e.RowIndex].Cells["员工姓名"].Value.ToString();                                                 
                        break;
                    case "yuangongfx":
                        MessageBox.Show("当前为复选窗口,请勾选前面的选择框,并点击确定按钮!","系统提示");
                        return;
                        break;
                    case "zhuangmogong":
                        this._UserId = dataGridView1.Rows[e.RowIndex].Cells["员工编号"].Value.ToString();
                        this._UserName = dataGridView1.Rows[e.RowIndex].Cells["员工姓名"].Value.ToString();
                        break;
                    case "gongyingshang":
                        this._UserId = dataGridView1.Rows[e.RowIndex].Cells["供应商编号"].Value.ToString();
                        this._UserName = dataGridView1.Rows[e.RowIndex].Cells["供应商名称"].Value.ToString();
                        break;
                    case "sbz":
                        this._UserId = dataGridView1.Rows[e.RowIndex].Cells["设备组"].Value.ToString();
                        break;
                    case "shebei":
                        this._UserId = dataGridView1.Rows[e.RowIndex].Cells["设备编号"].Value.ToString();
                        break;
                    case "gongyi":
                        this._UserId = dataGridView1.Rows[e.RowIndex].Cells["工艺"].Value.ToString();
                        break;
                    default:
                        break;
                }
                // 引发事件
                this.DataSelected(null, null);
            }
            this.Close();  // 关闭本窗体

        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.DataSelected != null)
                {

                    try
                    {
                        switch (kclx)
                        {
                            case "yuangongfx":
                                for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
                                {
                                    if (Convert.ToBoolean(this.dataGridView1.Rows[i].Cells["colCB"].Value))
                                    {
                                        this._UserIds.Add(dataGridView1.Rows[i].Cells["员工编号"].Value.ToString());
                                        this._UserNames.Add(dataGridView1.Rows[i].Cells["员工姓名"].Value.ToString());
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    catch
                    {

                    }
                    // 引发事件
                    this.DataSelected(null, null);
                }
            }
            catch
            {
                this.DataSelected(null, null);
            }
                this.Close();  // 关闭本窗体
            

        }
    }

   
}
